#!/bin/sh
#
# pcp-qa-summary [vm ...]
#
# Used in concert with pcp-daily -q
#

_usage()
{
    echo "Usage: $0 [options] [vm ...]"
    echo 'Options:'
    echo '  -a      report all tests, not just failing ones (implies -n)'
    echo '  -c      report counts not percentages'
    echo '  -d      debug'
    echo '  -f      sort failure map on %fail [default sort on Test #]'
    echo '  -m N    minimum number of tests run to be reported [default 60]'
    echo '  -n      report skipped and not run tests (reported as "S" and "N")'
    echo '  -r      only most recent results date [default report all dates]'
    exit 1
}

_warn_about_BAD()
{
    if [ ! -f $HOME/src/pcp/qa/group ]
    then
	echo "Arrgh, no $HOME/src/pcp/qa/group ... do you know what you're doing?"
	sts=1
	exit
    else
	grep '^[0-9][^ ]* .*BAD' $HOME/src/pcp/qa/group \
	| sed >$tmp.bad \
	    -e '/^[0-9][0-9]*:/d' \
	# end
	if [ -s $tmp.bad ]
	then
	    echo
	    echo "Warning: the following tests have not been run at all because they do not"
	    echo "         satisfy the cross-platform robustness criteria."
	    echo "         (refer to man/html/portability-and-qa.html)"
	    sed -e 's/^/    /' <$tmp.bad
	fi
    fi
}

# must have tried to run at least this number to qualify
# 40 = > -g sanity circa PCP 2.7.8 [minus a few not runs]
#
minrun=40

debug=false
recent=false
aflag=false
cflag=false
nflag=false
failsort=false
while getopts 'acdfm:nr?' p
do
    case "$p"
    in
	a)	aflag=true
		nflag=true
		;;
	c)	cflag=true
		;;
	d)	debug=true
		;;
	f)	failsort=true
		;;
	m)	minrun=$OPTARG
		;;
	n)	nflag=true
		;;
	r)	recent=true
		;;
	?)	_usage
		# NOTREACHED
    esac
done
shift `expr $OPTIND - 1`

sts=0
if $debug
then
    tmp=`pwd`/tmp-summary
    trap "exit \$sts" 0 1 2 3 15
else
    tmp=/var/tmp/$$
    trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15
fi
rm -f $tmp.*

if [ $# -eq 0 ]
then
    set -- `[ -d $HOME/Logs/by-vm ] && cd $HOME/Logs/by-vm && echo *`
fi

if [ -f $HOME/whatami.out ]
then
    # Turn
    # Host        PCP      CPU     Operating System
    # ----------- -------- ------  ------------------
    # vm00        3.7.0    x86_64  Ubuntu 12.04 (precise)
    # into
    # vm00|PCP 3.7.0 x86_64 Ubuntu 12.04
    #
    sed <$HOME/whatami.out \
	-e '1,2d' \
	-e '/^[ 	]*$/d' \
	-e 's/  */|PCP#/' \
	-e 's/  */#/' \
	-e 's/  */#/' \
	-e 's/  *(.*)//' \
	-e 's/#/ /g' \
    | LC_COLLATE=POSIX sort -t '|' --key=1,1 >$tmp.what
fi

_what()
{
    if [ -f $tmp.what ]
    then
	LC_COLLATE=POSIX sort -t '|' --key=1,1 \
	| join \
	    -1 2 -2 1 -t '|' -a 1 -e "$vmdir" -o 1.1,2.1,2.2 \
	    - $tmp.what \
	| awk -F\| '
BEGIN	{ OFS ="|" }
	{ if ($2 ~ /^vm/)
	    $2 = substr($2,3,2) "|" $2
	  else if ($2 ~ /^bozo-laptop/)
	    $2 = "bl" "|" $2
	  else if ($2 ~ /^bozo-vm/)
	    $2 = "bv" "|" $2
	  else
	    $2 = substr($2,1,2) "|" $2
	}
	{ print }' \
	| sed -e 's/|PCP/ PCP/'
    else
	cat
    fi
}

echo "==== QA Summary ===="
echo
echo "Date        Run Pass Fail Nrun Host"
numqahosts=0
for vm
do
    cd $HOME/Logs/by-vm
    if [ -d "$vm" ]
    then
	cd "$vm"
	vmdir="$vm"
    else
	vmdir=`echo ${vm}*`
	if [ -d "$vmdir" ]
	then
	    cd "$vmdir"
	else
	    echo "$vm: arrrgggh no \$HOME/Logs/${vm}* directory"
	    continue
	fi
    fi
    basevmname=`echo $vmdir | sed -e 's/\(vm[0-9][0-9]\)-.*/\1/' -e 's/\.sgi.com//'`
    rm -f $tmp.seen $tmp.found
    for date in `ls | grep -v qa | grep -v pcpqa | grep -v pcp | grep -v whatami | sort -r`
    do
	[ -f $date ] || continue
	touch $tmp.seen
	$debug && echo "Debug: == $vmdir on $date =="
	# Looking for
	# Not run: 008 009 022 096 114 117 130 148 164 212 223 233 284 290 307 311 329 330 331 332 333 348 361 370 377 390 420 441 442 476 477 478 480 506 512 545 570 584 640 652 654 712 713 714
	# Failures: 062 169 244
	# Failed 3 of 516 tests
	# Passed all 67 tests
	#
	rm -f $tmp.dbg
	if egrep '^((Not run:)|Failures:|(Failed [0-9][0-9]* of [0-9][0-9]* tests)|(Passed all [0-9][0-9]* tests)|Info:|===)' <$date >/dev/null
	then
	    awk <$date >$tmp.out '
/^Not run:/	{ notrun = NF - 2
		  nrlist = $3
		  for (i = 4; i <= NF; i++) nrlist = nrlist " " $i
		  state = 1
		  print NR,": Not run: start: notrun=" notrun >"'$tmp.dbg'"
		  next
		}
/^Failures:/	{ nfail = NF - 1
		  flist = $2
		  for (i = 3; i <= NF; i++) flist = flist " " $i
		  print NR,": Failures: start: nfail=" nfail >"'$tmp.dbg'"
		  state = 2
		  next
		}
/^Failed [0-9][0-9]* of [0-9][0-9]* tests/	{
		  failed = $2
		  run = $4
		  state = 3
		}
/^Passed all [0-9][0-9]* tests/	{
		  failed = 0
		  run = $3
		  print NR,": Passed all: " run >"'$tmp.dbg'"
		  state = 3
		}
state == 3	{ if (run >= '$minrun') printf "%s %4d %4d %4d %4d|%s|%s+%s\n","'$date'",run,run-failed,failed,notrun,"'$basevmname'",flist,nrlist
		  state = 0
		  notrun = 0
		  nfail = 0
		}
/^Info:/	{
		  state = 0
		  notrun = 0
		  nfail = 0
		  next
		}
/^===/		{
		  state = 0
		  notrun = 0
		  nfail = 0
		  next
		}
state == 1	{ # Not run: list broken over multiple lines
		  notrun += NF
		  for (i = 1; i <= NF; i++) nrlist = nrlist " " $i
		  print NR,": Not run: add",NF," notrun=" notrun >"'$tmp.dbg'"
		  next
		}
state == 2	{ # Failures: list broken over multiple lines
		  nfail += NF
		  for (i = 1; i <= NF; i++) flist = flist " " $i
		  print NR,": Failures: add",NF," nfail=" nfail >"'$tmp.dbg'"
		  next
		}
END		{ print NR,": END state=" state >"'$tmp.dbg'"
		}'
	    # Need to sort multiple runs one this date and pick the one
	    # with the maximum passes and minimum not runs
	    #
	    # 2013-03-13      1      1      0      0|vm19-opensuse-64
	    # 2013-03-13     67     67      0      2|vm19-opensuse-64
	    # 2013-03-13    512    509      3     48|vm19-opensuse-64
	    # 2013-03-13    512    509      3     16|vm19-opensuse-64
	    $debug && sed -e 's/^/Debug: line:/' <$tmp.dbg
	    sort <$tmp.out --key=3,3nr --key=5,5n \
	    | sed -e 1q >$tmp.tmp
	    # $tmp.tmp line is ...
	    # <date> <#run> <#passed> <#failed> <#notrun>|<hostname>|<failed list...>+<passed list>
	    if [ -s $tmp.tmp ]
	    then
		if $debug
		then
		    echo "Debug: summary lines ..."
		    sed -e 's/^/Debug: /' <$tmp.out
		fi
		sed -e 's/|[^|]*$//' <$tmp.tmp | _what >>$tmp.found
		nfail=`awk <$tmp.tmp '{print $4}'`
		if [ $nfail -gt 0 ]
		then
		    sed -e 's/.*|//' -e 's/+.*//' <$tmp.tmp \
		    | tr ' ' '\012' \
		    | sed -e "s/\$/ X $basevmname/" >>$tmp.map
		fi
		nnotrun=0
		if $nflag
		then
		    nnotrun=`sed -e 's/|.*//' <$tmp.tmp | awk '{print $5}'`
		    if [ $nnotrun -gt 0 ]
		    then
			sed -e 's/.*+//' <$tmp.tmp \
			| tr ' ' '\012' \
			| sed -e "s/\$/ N $basevmname/" >>$tmp.map
		    fi
		fi
		if $aflag || [ $nfail -gt 0 -o $nnotrun -gt 0 ]
		then
		    # pass lines look like:
		    # [97%] 1055 16s ...
		    # or
		    # [97%] 1055
		    # and may contain " [fixup]"
		    #
		    sed <$date -n \
				-e 's/ \[fixup]//' \
		    		-e '/^[[0-9][0-9]*%] [0-9][0-9]* [0-9][0-9]*s \.\.\./{
s/.*] //
s/ .*//
p
}' \
				-e '/^[[0-9][0-9]*%] [0-9][0-9]*$/{
s/.*] //
p
}' \
		    | sed -e "s/\$/ P $basevmname/" >>$tmp.map
		fi
		$recent && break
	    else
		$debug && echo "Debug: tag lines, but no QA results found"
	    fi
	else
	    $debug && echo "Debug: no QA result tag lines found"
	fi
    done
    if [ -f $tmp.found ]
    then
	sort $tmp.found
	cat $tmp.found >>$tmp.counts
	numqahosts=`expr $numqahosts + 1`
    else
	if $recent
	then
	    : be quiet
	else
	    if [ -f $tmp.seen ]
	    then
		printf "%-30s|%s\n" "Daily runs, but no QA" "$basevmname" | _what
	    elif [ ! -f $tmp.seen ]
	    then
		printf "%-30s|%s\n" "No daily runs" "$basevmname" | _what
	    fi
	fi
    fi

done

if [ -f $tmp.counts ]
then
    awk <$tmp.counts '
	{ nrun += $2; nfail += $4 }
END	{ printf "Summary: %d run, %d failed (%.2f%%)\n", nrun, nfail, 100*nfail/nrun }'
fi

_warn_about_BAD

if [ -f $tmp.map ]
then
    echo
    if $nflag
    then
	echo "==== QA Failure (X), Not Run (N) and Skipped (S) Map ===="
    else
	echo "==== QA Failure (X) and Not Run or Skipped (-) Map ===="
    fi
    echo
    ( ( sed -e 's/.* //' <$tmp.map | sort | uniq ) \
      ; echo "---" \
      ; sort -n $tmp.map \
      ; echo "END" \
    ) \
    | awk '
function heading() {
    printf "%4.4s","Host"
    printf " %5.5s",""		# %fail or nfail (-c)
    if (nflag) {
	printf " %5.5s",""	# %pass or npass (-c)
	printf " %5.5s",""	# %nrun or nnrun (-c)
    }
    for (i = 0; i < nh; i++) {
	if (host[i] ~ /^vm/)
	    printf " %2.2s",substr(host[i],length(host[i])-1)
	else if (host[i] == "bozo-laptop")
	    printf " %2.2s","bl"
	else if (host[i] == "bozo-vm")
	    printf " %2.2s","bv"
	else
	    printf " %2.2s",substr(host[i],1,2)
    }
    printf "\n"
    printf "%4.4s","Test"
    if (cflag)
	printf " %5.5s"," fail"
    else
	printf " %5.5s","%fail"
    if (nflag) {
	if (cflag) {
	    printf " %5.5s"," pass"
	    printf " %5.5s"," nrun"
	}
	else {
	    printf " %5.5s","%pass"
	    printf " %5.5s","%nrun"
	}
    }
    for (i = 0; i < nh; i++) printf " %2.2s"," "
    printf " %4.4s QA groups\n","Test"
}
BEGIN	{ nh = 0
	  state = 0
	  aflag = 0
	  if ("'"$aflag"'" == "true") aflag = 1
	  cflag = 0
	  if ("'"$cflag"'" == "true") cflag = 1
	  nflag = 0
	  if ("'"$nflag"'" == "true") nflag = 1
	}
$1 == "---"	{ heading()
		  state = 1
		  last = $1
		  next
		}
state == 0	{ host[nh] = $1; nh++; next }
$1 != last	{ if (last != "---") {
		      skip = nrun = bad = pass = 0
		      for (i = 0; i < nh; i++) {
			  if (fail[i] == "X") bad++
			  if (fail[i] == "P") {
			      pass++
			      fail[i] = " "
			  }
			  if (fail[i] == "N") {
			      nrun++
			      if (nflag == 0) fail[i] = "-"
			  }
			  if (fail[i] == "S") {
			      skip++
			      if (nflag == 0) fail[i] = "-"
			  }
		      }
		      if (bad > 0 || aflag) {
			  printf "%04d",last
			  if (nflag && nrun == nh) {
			    printf "   NOT RUN AT ALL "
			  }
			  else {
			      if (cflag)
				  printf "  %4d",bad
			      else
				  printf "  %3d%%",int(0.5+100*bad/'$numqahosts')
			      if (nflag) {
				  if (cflag) {
				      printf "  %4d",pass
				      printf "  %4d",nrun+skip
				  }
				  else {
				      if (pass > 0)
					  printf "  %3d%%",int(0.5+100*pass/'$numqahosts')
				      else
					  printf "  %3d%%",0
				      if (nrun > 0)
					  printf "  %3d%%",int(0.5+100*(nrun+skip)/'$numqahosts')
				      else
					  printf "  %3d%%",0
				  }
			      }
			  }
			  for (i = 0; i < nh; i++) {
			      printf " %2.2s",fail[i]
			  }
			  printf " %4.4s ",last
			  cmd = "( grep \"^" last " \" $HOME/src/pcp/qa/group || echo \"??? missing in QA group file\" ) | sed -e \"s/^[0-9]* //\""
			  system(cmd)
		      }
		  }
		  for (i = 0; i < nh; i++) fail[i] = "S"
		  last = $1
		 }
$1 == last	{ for (i = 0; i < nh; i++) {
		    if (host[i] == $3) {
			fail[i] = $2
			break
		    }
		  }
		}' \
    | sed \
	-e 's/ remote//' \
	-e 's/ sanity//' \
	-e 's/ ostest//' \
	-e 's/ local//' \
	-e 's/ oss//' \
	-e 's/ #[0-9][0-9]*//g' >$tmp.mapout
    num=0
    if $failsort
    then
	sed -e 2q <$tmp.mapout
	sed -e 1,2d <$tmp.mapout \
	| sort --key=2,2nr --key=1,1n 
    else
	cat $tmp.mapout
    fi \
    | while read line
    do
	if [ "$num" -eq 20 ]
	then
	    sed -e 2q <$tmp.mapout
	    num=0
	else
	    num=`expr $num + 1`
	fi
	echo "$line"
    done \
    | sed -e 's/^0/ /'
    sed -n -e 2p -e 2q <$tmp.mapout
    sed -n -e 1p -e 1q <$tmp.mapout
fi

